//
//  QRCodeViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/23.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "QRCodeViewController.h"
#import "SGQRCode.h"
#import "Context.h"

@interface QRCodeViewController ()

@property (strong, nonatomic) UIImageView *qrcodeImageView;

@end

@implementation QRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_BACKGROUND;
    [self setBackNavgationItem];
    self.navigationItem.title = @"我的二维码";
    
    [self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
// 生成二维码
- (void)generateQRCodeForText:(NSString *)text {
    // 1、借助UIImageView显示二维码
    UIImageView *imageView = [[UIImageView alloc] init];
    CGFloat imageViewW = SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f);
    CGFloat imageViewH = imageViewW;
    CGFloat imageViewX = (self.view.frame.size.width - imageViewW) / 2;
    CGFloat imageViewY = TRANS_VALUE(30.0f);
    imageView.frame =CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    [self.view addSubview:imageView];
    
    // 2、将CIImage转换成UIImage，并放大显示
    imageView.image = [SGQRCodeTool SG_generateWithDefaultQRCodeData:text imageViewWidth:imageViewW];
    
#pragma mark - - - 模仿支付宝二维码样式（添加用户头像）
    CGFloat scale = 0.22;
    CGFloat borderW = 5;
    UIView *borderView = [[UIView alloc] init];
    CGFloat borderViewW = imageViewW * scale;
    CGFloat borderViewH = imageViewH * scale;
    CGFloat borderViewX = 0.5 * (imageViewW - borderViewW);
    CGFloat borderViewY = 0.5 * (imageViewH - borderViewH);
    borderView.frame = CGRectMake(borderViewX, borderViewY, borderViewW, borderViewH);
    borderView.layer.borderWidth = borderW;
    borderView.layer.borderColor = [UIColor purpleColor].CGColor;
    borderView.layer.cornerRadius = 10;
    borderView.layer.masksToBounds = YES;
    borderView.layer.contents = (id)[UIImage imageNamed:@"ic_avatar_default"].CGImage;
    
    //[imageView addSubview:borderView];
}

#pragma mark - Private Method
- (void)createUI {
    //生成二维码
    NSString *mobile = [Context sharedInstance].mobile;
    NSString *urlStr = [NSString stringWithFormat:@"http://www.hwzb666.com:8080/web/pay.jsp?loginid=%@", mobile];
    [self generateQRCodeForText:urlStr];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(290.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    titleLabel.textColor = I_COLOR_BLACK;
    titleLabel.text = @"我的二维码";
    [self.view addSubview:titleLabel];
}


@end
